﻿using System;
using System.Collections.Generic;
using System.IO;

namespace AutoExecuteSp
{
    public class ScriptPicker
    {
        public List<FileInfo> PickAllFile()
        {
            DateTime? time = ConfigReader.GetValue<DateTime?>("StartDate");
            DateTime? time2 = ConfigReader.GetValue<DateTime?>("EndDate");

            return GetAllSqlDbText(time.GetValueOrDefault(DateTime.Now), time2.GetValueOrDefault(DateTime.Now));
        }

        private List<FileInfo> GetAllSqlDbText(DateTime stratDate, DateTime endDate)
        {
            string str = ConfigReader.GetValue<string>("DBFilePath").Trim();
            string str2 = ConfigReader.GetValue<string>("DBFileBasePath").Trim();

            var fileInfos = new List<FileInfo>();

            string[] strArray = str.Split(new[] { ',' });
            var list = new List<string>();

            foreach (string str3 in strArray)
            {
                if (!string.IsNullOrEmpty(str3))
                {
                    list.Add(Path.Combine(str2, str3));
                }
            }

            foreach (string str3 in list)
            {
                var info = new DirectoryInfo(str3);

                if (!info.Exists) continue;

                foreach (FileInfo info2 in info.GetFiles("*.sql"))
                {
                    if ((info2.LastWriteTime < stratDate) || (info2.LastWriteTime >= endDate.AddDays(1.0))) continue;

                    fileInfos.Add(info2);
                }
            }

            return fileInfos;
        }
    }
}
